package issr.market.db.impl;

import issr.market.db.DBConnector;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class DBConnectorImpl implements DBConnector {

	private static Connection activeConnection = null;

	public Connection getDBConnection() throws SQLException {
		
		if (activeConnection == null || activeConnection.isClosed()) {
			try {

				System.out.println("Creating connection with: "
						+ "jdbc:mysql://" + DB_HOST + ":3306/" + DB_NAME
						+ "?user=" + DB_USER + "&password=" + DB_PWD);

				Driver.class.newInstance();
			} catch (IllegalAccessException e) {
				System.out.println(e.getMessage());
			} catch (InstantiationException e) {
				System.out.println(e.getMessage());
			}

			activeConnection = DriverManager.getConnection("jdbc:mysql://"
					+ DB_HOST + ":3306/" + DB_NAME + "?user=" + DB_USER
					+ "&password=" + DB_PWD);
		}

		return activeConnection;
	}
}
